package com.tencent.wxcloudrun.uitls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/14 11:39
 * @description: TODO
 */
public class JsonToSql {
    public static void main(String[] args) {
        String json = "[{\n" +
                "    \"allowOverflow\": false,\n" +
                "    \"allowRepetBuy\": true,\n" +
                "    \"dateAdd\": \"2020-09-01 13:12:42\",\n" +
                "    \"dateEnd\": \"2120-09-01 13:12:37\",\n" +
                "    \"id\": 2827,\n" +
                "    \"numberDoing\": 0,\n" +
                "    \"numberGoods\": 9999,\n" +
                "    \"numberOrder\": 3,\n" +
                "    \"numberPersion\": 3,\n" +
                "    \"numberSucccess\": 2,\n" +
                "    \"openUserType\": 0,\n" +
                "    \"openerSendMoney\": 0.00,\n" +
                "    \"openerSendScore\": 0,\n" +
                "    \"refundType\": 1,\n" +
                "    \"refundTypeStr\": \"原路退回\",\n" +
                "    \"status\": 0,\n" +
                "    \"timeoutHours\": 24\n" +
                "  }]";
        JSONArray objects = JSON.parseArray(json);
        for (int i = 0; i < objects.size(); i++) {
            JSONObject jsonObject = objects.getJSONObject(i);
            String s = sqlinsert(jsonObject, "banner"  );
            System.out.println(s);

        }
    }

    private static String sqlinsert(JSONObject lineJson, String tableName) {
        // 先转成 insert into tableName (若干字段）形式
        StringBuilder sb = new StringBuilder("INSERT INTO ")
                .append(tableName)
                .append(" (");
        for (String key : lineJson.keySet()) {
            if ("deleteField".equals(key)) {
                continue;
            }
            sb.append(key).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        // 转成 VALUES("内容〞;形式
        sb.append(") VALUES ( ");
        for (String key1 : lineJson.keySet()) {
            if ("deleteField".equals(key1)) {
                continue;
            }
            sb.append("\'")
                    .append(lineJson.getString(key1))
                    .append("\'");
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")").append(";");
        return sb.toString();

    }
}

